/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.theydream.qeechain.deprecated;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.theydream.qeechain.R;
import com.theydream.qeechain.collection.PetInfo;
import com.theydream.qeechain.globaldata.ItemListClass;


/**
 * Show all collection with list view
 */
public class AllPets extends ListActivity {
    //private static final String[] DATA={"AAA","BBB","CCC"};
    //private EfficientAdapter mEfficientAdapter;
	static Context mContext;
	private static ItemListClass itemListClass=ItemListClass.getInstance();
	boolean shown=false;
	

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        int listSize=itemListClass.getItemList().size();
        
        

        
        //for (int i=0;i<)
        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mContext=context;
        	mInflater = LayoutInflater.from(context);
        	
            // Icons bound to the rows.
            //mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.no_image);
            //Attraction.
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return itemListClass.getItemList().size();
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;
            String[] DATA=new String[listSize];

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            //FavouriteList itemListClassList=FavouriteList.getInstance();
            //int listSize=FavouriteList.getFavouriteSpotId().size();
            
            for (int i=0; i<listSize;i++)
            {
            	DATA[i]=itemListClass.getItemList().get(i).getTitle();
            	
            }
            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            Drawable drawable=mContext.getResources().getDrawable(mContext.getResources().getIdentifier(itemListClass.getItemList().get(position).getPic(),"drawable","com.theydream.qeechain"));
           
            if (null!=drawable)
            {
            	holder.icon.setImageDrawable(drawable);
            }
            else
            {
            	holder.icon.setImageResource(R.drawable.no_image);
            }
            

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            ImageView icon;
            
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EfficientAdapter mAdapter=new EfficientAdapter(this);
        setListAdapter(mAdapter);
        ListView lv = getListView();
        lv.setClickable(true);
        lv.setOnItemClickListener(new OnItemClickListener(){
        	public void onItemClick(AdapterView<?> mAdapter, View view,int position, long id){
        		Intent intent=new Intent(AllPets.this,PetInfo.class);
        		startActivity(intent);
//        		Random rand = new Random(System.currentTimeMillis());
//        		Toast.makeText(view.getContext(), "Level "+ rand.nextInt((int) (System.currentTimeMillis()%10)), Toast.LENGTH_SHORT).show();
//        		Intent infoIntent =new Intent(Favourite.this,AttractionInfo.class);
//        		Bundle bundle = new Bundle();
//        		Gson gson=new Gson();
//        		ItemListClass itemListClass=ItemListClass.getInstance();
//				bundle.putString("gsonAttrInfo",gson.toJson(itemListClass.getItemList().get(position)));
//            	//bundle.putString("jObject", objectDetails);
//            	infoIntent.putExtras(bundle);
//            	startActivity(infoIntent);
        	}
        });
        if (itemListClass.getItemList().size()<1)
        {
        	AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    		dialog.setTitle("No Record");
    		dialog.setMessage("Search For them in Map First");
    		shown=true;
    		dialog.show();
        }
 
        
    }
    
    @Override
    public void onResume()
    {	
    	super.onResume();
    	EfficientAdapter mAdapter=new EfficientAdapter(this);
        setListAdapter(mAdapter);
        ListView lv = getListView();
        lv.setClickable(true);
        lv.setOnItemClickListener(new OnItemClickListener(){
        	public void onItemClick(AdapterView<?> mAdapter, View view,int position, long id){
//        		Random rand = new Random(10);
//        		Toast.makeText(view.getContext(), "Level "+ rand.nextInt(10), Toast.LENGTH_SHORT).show();
        		Intent intent=new Intent(AllPets.this,PetInfo.class);
        		startActivity(intent);
        	}
        });
        if (itemListClass.getItemList().size()<1 & !shown)
        {
        	AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    		dialog.setTitle("No Record");
    		dialog.setMessage("Search For them in Map First");
    		dialog.show();
        }
        shown=false;
    }
    
//    protected void onListItemClick(ListView l, View v, int position, long id) 
//    {    
//       Toast.makeText(v.getContext(), "xxxx", Toast.LENGTH_SHORT);
//    }

}
